$(function() {
	//删除用户
	$("#key_list").on("click", ".remove-key", function() {
		var data = dataTablesReload.row($(this).parents('tr')).data();
		$.ajax({
			type: 'POST',
			url: "http://127.0.0.1:8080/key/deleteKey",
			dataType: 'json',
			data: {
				"id": data.id
			},
			xhrFields: {
				withCredentials: true
			},
			success: function(data) {
				if (data.code == 'success') {
					dataTablesReload.ajax.reload(null, false);
				}
				notifyMsg(data.msg);
			}
		})
	})

	//进入修改密码界面
	$("#key_list").on("click", ".edit-key", function() {
		var data = dataTablesReload.row($(this).parents('tr')).data();
		$.ajax({
			type: 'POST',
			url: "http://127.0.0.1:8080/key/getKeyInfo",
			dataType: 'json',
			data: {
				"id": data.id
			},
			success: function(data) {
				var key = data.data;
				$("#password").val(key.password);
				$("#keyId").val(key.id);
				$("#addRowModal").modal('show');
			}
		})
	})

	//新增或修改
	$("#addRowButton").click(function() {
		var id = $("#keyId").val();
		var password = $("#password").val();
		if (password == '') {
			notifyMsg("请输入密码");
			return;
		}
		if (password.length < 6) {
			notifyMsg("密码最少需要6位，请补足");
			return;
		}
		$.ajax({
			type: "POST",
			url: "http://127.0.0.1:8080/key/saveOrUpdate",
			dataType: "json",
			data: {
				"id": id,
				"password": password
			},
			xhrFields: {
				withCredentials: true
			},
			success: function(data) {
				var code = data.code;
				if (code == 'success') {
					$("#addRowModal").modal('hide');
					dataTablesReload.ajax.reload(null, false);
				}
				notifyMsg(data.msg);
			}
		});
	})

	//列表显示
	var dataTablesReload = $('#basic-datatables').DataTable({
		language: {
			"sLengthMenu": "显示 _MENU_ 项结果",
			"sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
			"sSearch": "搜索:",
			"oPaginate": {
				"sPrevious": "上页",
				"sNext": "下页"
			}
		},
		pageLength: 10,
		pagingType: "simple",
		bFilter: false,
		bLengthChange: true,
		serverSide: true,
		ordering: false,
		ajax: function(data, callback, settings) {
			var pagesize = data.length;
			var page = (data.start) / data.length + 1;
			$.ajax({
				type: "POST",
				url: "http://127.0.0.1:8080/key/findKeyListByPage",
				cache: false,
				xhrFields: {
					withCredentials: true
				},
				data: {
					"pageNumber": page,
					"pageSize": pagesize
				},
				dataType: "json",
				success: function(data) {
					console.log(data)
					var code = data.code;
					if (code == 'success') {
						var returnData = {};
						returnData.recordsTotal = data.data.count;
						returnData.recordsFiltered = data.data.count;
						returnData.data = data.data.keyList;
						callback(returnData);
					} else {
						notifyMsg(data.msg);
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					notifyMsg('获取分页数据失败');
				}
			});
		},
		columns: [{
				"data": "password",
				"defaultContent": "<i></i>"
			},
			{
				"data": "user.userName",
				"defaultContent": "<i></i>"
			},
			{
				"data": "updateDate",
				"defaultContent": "<i></i>",
				"render": function ChangeDateFormat(date) {
					if (date == null) {
						return "";
					} else {
						var dateee = new Date(date).toJSON();
						return new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/,
							'');
					}
				}
			},
			{
				"data": "",
				"defaultContent": '<div class="form-button-action"><button type="button" data-toggle="tooltip"' +
					' title="" class="btn btn-link btn-primary btn-lg edit-key" data-original-title="修改密码">' +
					'<i class="fa fa-edit"></i></button><button type="button" data-toggle="tooltip"' +
					' title="" class="btn btn-link btn-danger remove-key" data-original-title="删除密码"><i class="fa fa-times">' +
					'</i></button></div>'
			},
		]
	});
});
